package com.jackrain.nea.fc.ps.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.ViewCskuarchCmd;
import com.jackrain.nea.fc.ps.mapper.ViewCskuMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author xuxinshuai
 * @date 2017/10/20
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ps")
public class ViewCskuarchCmdImpl extends CommandAdapter implements ViewCskuarchCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        ViewCskuMapper viewCskuMapper = ApplicationContextHandle.getBean(ViewCskuMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || param.size() == 0|| param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        log.info("ViewCskuarchCmdImpl--param-->{}",param);
//        log.debug("----------条码查询参数----------"+param);
        List<HashMap> mapList = new ArrayList<>();
        mapList = viewCskuMapper.selectskuarchsql(param.getJSONArray("IDS").toArray());
        JSONArray jsonArray = listToJsonarray(mapList);

        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);
            //配销中心
            if (jsonObject.getString("CP_C_DISTRIB_ID") != null) {
                long storeid = jsonObject.getLong("CP_C_DISTRIB_ID");
                String storecode = viewCskuMapper.selectstoreecode(storeid);
                jsonObject.put("CP_C_DISTRIB_ID", storecode);
            }
            //商品
            Long proID = null;
            if (jsonObject.getString("PS_C_PRO_ID") != null) {
                long cproid = jsonObject.getLong("PS_C_PRO_ID");
                proID = cproid;
                String cproecode = viewCskuMapper.selectproecode(cproid);
                jsonObject.put("PS_C_PRO_ID", cproecode);
            }
            //颜色
            if (jsonObject.getString("PS_C_SPEC1OBJ_ID") != null) {

                Long specobjid = jsonObject.getLong("PS_C_SPEC1OBJ_ID");
                String ecodeStr = viewCskuMapper.selectspececood(specobjid);
                jsonObject.put("CRL_ECODE", ecodeStr);

            } else {
                jsonObject.put("CRL_ECODE", "");
            }
            //尺寸
            if (jsonObject.getString("PS_C_SPEC2OBJ_ID") != null) {
                Long specobjid = jsonObject.getLong("PS_C_SPEC2OBJ_ID");
                String ecodeStr = viewCskuMapper.selectspececood(specobjid);
                jsonObject.put("SIZE_ECODE", ecodeStr);
            } else {
                jsonObject.put("SIZE_ECODE", "");
            }
            //商品性质
            if (jsonObject.getString("PRONATURE") != null) {
                long cproid = jsonObject.getLong("PRONATURE");
                String cproecode = viewCskuMapper.selectprodimecode(cproid);
                jsonObject.put("PRONATURE", cproecode);
            }
            //采购模式
            if (jsonObject.getString("PURCHASEMODE") != null) {
                long cproid = jsonObject.getLong("PURCHASEMODE");
                String cproecode = viewCskuMapper.selectprodimecode(cproid);
                jsonObject.put("PURCHASEMODE", cproecode);
            }
            //修改人
            if (jsonObject.getString("MODIFIERID") != null) {
                long userid = jsonObject.getLong("MODIFIERID");
                String ename = viewCskuMapper.selectusername(userid);
                jsonObject.put("ENAME", ename);
            }
            //停止使用
            //修改人

            if (proID != null) {
                //查询号型ID

                Long shapeGroupId = viewCskuMapper.selectshapegroup(proID);
                //根据PS
                // _C_SHAPEGROUP_ID和PS_C_SIZE_ID 查询PS_C_SHAPE的ECODE
                if (shapeGroupId!=null){
                    Long sizeId = jsonObject.getLong("PS_C_SPEC2OBJ_ID");
                    String shapeEcode = viewCskuMapper.selectshapeecode(shapeGroupId, sizeId);
                    jsonObject.put("PS_C_SHAPE", shapeEcode);
                }
            }
            jsonObject.remove("PS_C_SPEC1OBJ_ID");
            jsonObject.remove("PS_C_SPEC2OBJ_ID");
        }
        vh.put("data", jsonArray);
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("成功"));
        return vh;
    }

    /**
     * list转jsonarray
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {

        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }

}
